package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.xiaoymin.knife4j.core.util.CollectionUtils;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.mybatis.spring.SqlSessionTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    private static final Logger log = LoggerFactory.getLogger(SetmealServiceImpl.class);

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;

    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        log.info("Service层分页查询：{}", setmealPageQueryDTO);
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        List<SetmealVO> list = setmealMapper.page(setmealPageQueryDTO);
        Page<SetmealVO> page = (Page<SetmealVO>) list;
        log.info("Service层分页查询结果：{}", page);
        //通过套餐id获取套餐名称
        for (SetmealVO setmealVO : list) {
            setmealVO.setCategoryName(setmealMapper.getNameBycategoryId(setmealVO.getCategoryId()));
        }


        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishVO> getDishItemById(Long id) {
        log.info("Service层查询菜品选项开始");
        List<DishVO> dishItemBySetmealId = setmealMapper.getDishItemBySetmealId(id);
        log.info("Service层查询菜品选项结果：{}", dishItemBySetmealId);
        return dishItemBySetmealId;
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        log.info("Service层更新套餐状态开始");
        Setmeal setmeal =new Setmeal();
        setmeal.setId(id);
        setmeal.setStatus(status);
        setmealMapper.update(setmeal);
    }

    @Override
    @Transactional
    public void delete(List<Long> ids) {
        log.info("删除套餐，id为：{}", ids);
        setmealMapper.deleteByIds(ids);
        //删除套餐和菜品的关联数据
        setmealMapper.deleteSetmealDishBySetmealIds(ids);

    }

    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        //修改套餐数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.update(setmeal);


        //修改套餐菜品数据
        Long id = setmeal.getId();
        setmealMapper.deleteSetmealDishBySetmealId(id);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (!CollectionUtils.isEmpty(setmealDishes)) {
            setmealDishes.forEach(dish -> dish.setSetmealId(id));
            setmealMapper.insertBatch(setmealDishes);
        }
    }

    @Override
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        log.info("保存套餐信息:{}", setmealDTO);
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmeal.setStatus(StatusConstant.ENABLE);
        setmealMapper.insert(setmeal);
        //获取生成的套餐id
        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
        });
        setmealMapper.insertBatch(setmealDishes);
    }

    @Override
    public SetmealVO getByIdWithFlavor(Long id) {
        //查询分类信息
       SetmealVO setmealVO = setmealMapper.getById(id);
        log.info("查询到的套餐信息：{}", setmealVO);
        //查询菜品信息
        List<SetmealDish> setmealDishes = setmealMapper.getSetmealDishBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDishes);
        log.info("查询到的菜品信息：{}", setmealDishes);
        return setmealVO;
    }
}
